package view;

import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JList;
import java.awt.Dimension;
import javax.swing.JButton;
import model.*;
import java.util.*;

public class frmTipoLectura extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JTextField jTextField = null;

	private JLabel jLabel1 = null;

	private JButton btnCancelar = null;
	
	private Fachada jebi = null;

	private JButton btnAgregar = null;

	private JList lstMetaData = null;
	
	
	/**
	 * This is the default constructor
	 */
	public frmTipoLectura(Fachada unaF) {
		super();
		initialize();
		this.jebi = unaF;
		inicializo(unaF.getColMetadata());
	}

	public frmTipoLectura() {
		super();
		initialize();
		this.jebi = new Fachada();
		inicializo(null);// cambiar por null
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 249);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(21, 55, 68, 15));
			jLabel1.setText("Atributos");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(21, 24, 64, 15));
			jLabel.setText("Nombre");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getBtnCancelar(), null);
			jContentPane.add(getBtnAgregar(), null);
			jContentPane.add(getLstMetaData(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(111, 26, 147, 19));
		}
		return jTextField;
	}

	/**
	 * This method initializes btnCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancelar() {
		if (btnCancelar == null) {
			btnCancelar = new JButton();
			btnCancelar.setBounds(new Rectangle(164, 183, 80, 27));
			btnCancelar.setText("Salir");
		}
		return btnCancelar;
	}
	
	private void inicializo(ArrayList lista){
		this.btnCancelar.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent evt) {
	              btnCancelarActionPerformed(evt);
	            }
		});
		this.btnAgregar.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent evt) {
	              btnAgregarActionPerformed(evt);
	            }
		});
		//cargar el jlist
		if (lista == null){
			ArrayList inicializo = this.jebi.getColMetadata();
			this.lstMetaData.setListData(inicializo.toArray());
		}else {
			this.lstMetaData.setListData(lista.toArray());
		}
	}
	private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt){
		this.dispose();
	}

	/**
	 * This method initializes btnAgregar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnAgregar() {
		if (btnAgregar == null) {
			btnAgregar = new JButton();
			btnAgregar.setBounds(new Rectangle(46, 186, 82, 24));
			btnAgregar.setEnabled(true);
			btnAgregar.setText("Agregar");
		}
		return btnAgregar;
	}

	private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt){
		if(this.jebi.existeTipoLectura(this.jTextField.getText())){
			//mensaje de error
			disparoMensaje("Este tipo de Ficha ya Existe en el Sistema",JOptionPane.ERROR_MESSAGE);
		}else{
			//agrego el objeto a la base
			this.persistoTipoLectura();
			
			//disparoMensaje(this.lstMetaData.getSelectedValues().toString(),JOptionPane.PLAIN_MESSAGE);
			//mensaje de ok
			disparoMensaje("Los datos se ingresaron con Existo al Sistema",JOptionPane.PLAIN_MESSAGE);
		}
		
	}
	
	private void persistoTipoLectura(){
		try{
						
		this.jebi.altaTipoLectura(this.jTextField.getText(), this.lstMetaData.getSelectedValues());
		}catch (NullPointerException e) {
			this.disparoMensaje(e.getMessage().toString(), JOptionPane.ERROR_MESSAGE);
			//ver de solucionar el tema
		}
	}
	
	private void disparoMensaje(String str, int alerta){
		JOptionPane.showMessageDialog(null, str, "Mensaje del Sistema",alerta );
	}
	/**
	 * This method initializes lstMetaData	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getLstMetaData() {
		if (lstMetaData == null) {
			lstMetaData = new JList();
			lstMetaData.setBounds(new Rectangle(108, 60, 165, 116));
			lstMetaData.setSelectedIndex(-1);
		}
		return lstMetaData;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
